package org.light.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.domain.Controller;
import org.light.domain.Domain;
import org.light.domain.Field;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.ValidateInfo;
import org.light.exception.ValidateException;
import org.light.simpleauth.verb.ChangeMyPasswordUser;
import org.light.simpleauth.verb.FindMyProfileUser;
import org.light.simpleauth.verb.UpdateMyProfileUser;
import org.light.utils.DomainUtil;
import org.light.utils.WriteableUtil;

public class GinPlainProfileController extends GinController implements Comparable<Controller>{
	private static final long serialVersionUID = 6506582842158817100L;

	public GinPlainProfileController(Domain domain) throws ValidateException {
		super(domain);
		this.domain = domain;
	}

	public StatementList generateControllerStatements() throws ValidateException {
		List<Writeable> sList = new ArrayList<Writeable>();
		StringBuilder sb = new StringBuilder();
		sb.append("package ").append(this.getDomain().getControllerSuffix()).append("\n\n");
		Set<String> imports = new TreeSet<>();
		imports.add("github.com/gin-gonic/gin");
		imports.add("session \"github.com/ScottHuangZL/gin-jwt-session\"");
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),"pkg.errno"));		
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()));
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()));
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+".utils"));
		String importsStr = DomainUtil.generateImportStr(imports);
		sb.append(importsStr);
		
		sList.add(new Statement(1000L,0,sb.toString()));
		
		Set<Field> deniedFields = new TreeSet<>();
		deniedFields.add(this.domain.findFieldByFixedName("password"));
		deniedFields.add(this.domain.findFieldByFixedName("salt"));
		deniedFields.add(this.domain.findFieldByFixedName("loginFailure"));
		
		ChangeMyPasswordUser changePass = new ChangeMyPasswordUser(this.domain);
		FindMyProfileUser findProfile = new FindMyProfileUser(this.domain,deniedFields);
		UpdateMyProfileUser update = new UpdateMyProfileUser(this.domain,deniedFields);
		StatementList sl1 = changePass.generateControllerMethod().generateMethodFullStatements();
		StatementList sl2 = findProfile.generateControllerMethod().generateMethodFullStatements();
		StatementList sl3 = update.generateControllerMethod().generateMethodFullStatements();
		sl1.setSerial(2000L);
		sl2.setSerial(3000L);
		sl3.setSerial(4000L);
		sList.add(sl1);
		sList.add(sl2);
		sList.add(sl3);
		return WriteableUtil.merge(sList);
	}
	
	@Override
	public String generateControllerString() throws ValidateException {
		return generateControllerStatements().getContent();
	}

	public ValidateInfo validate() {
		ValidateInfo info = new ValidateInfo();
		info.setSuccess(true);
		if (this.verbs == null || this.verbs.size() == 0) {
			info.setSuccess(false);
			info.addCompileError("Controller " + this.standardName
					+ " 动词是空！");
		}
		for (Verb v : this.verbs) {
			if (v != null && v.getDomain() == null) {
				info.setSuccess(false);
				info.addCompileError("Controller " + this.standardName + "动词"
						+ v.getVerbName() + "域对象是空值！");
			}
		}
		return info;
	}
}
